package org.dromara.third.auth.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.third.auth.domain.AuthCompanyInfo;
import org.dromara.third.auth.domain.bo.AuthCompanyInfoBo;
import org.dromara.third.auth.domain.vo.AuthCompanyInfoVo;
import org.dromara.third.auth.mapper.AuthCompanyInfoMapper;
import org.dromara.third.auth.service.IAuthCompanyInfoService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 企业权限Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-18
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AuthCompanyInfoServiceImpl implements IAuthCompanyInfoService {

    private final AuthCompanyInfoMapper baseMapper;

    /**
     * 查询企业权限
     *
     * @param id 主键
     * @return 企业权限
     */
    @Override
    public AuthCompanyInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询企业权限列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 企业权限分页列表
     */
    @Override
    public TableDataInfo<AuthCompanyInfoVo> queryPageList(AuthCompanyInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AuthCompanyInfo> lqw = buildQueryWrapper(bo);
        Page<AuthCompanyInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的企业权限列表
     *
     * @param bo 查询条件
     * @return 企业权限列表
     */
    @Override
    public List<AuthCompanyInfoVo> queryList(AuthCompanyInfoBo bo) {
        LambdaQueryWrapper<AuthCompanyInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AuthCompanyInfo> buildQueryWrapper(AuthCompanyInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AuthCompanyInfo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AuthCompanyInfo::getId);
        lqw.eq(bo.getAppId() != null, AuthCompanyInfo::getAppId, bo.getAppId());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyCode()), AuthCompanyInfo::getCompanyCode, bo.getCompanyCode());
        lqw.like(StringUtils.isNotBlank(bo.getEnterpriseName()), AuthCompanyInfo::getEnterpriseName, bo.getEnterpriseName());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyIp()), AuthCompanyInfo::getCompanyIp, bo.getCompanyIp());
        lqw.eq(StringUtils.isNotBlank(bo.getSecret()), AuthCompanyInfo::getSecret, bo.getSecret());
        lqw.eq(bo.getCuserId() != null, AuthCompanyInfo::getCuserId, bo.getCuserId());
        lqw.eq(bo.getCtime() != null, AuthCompanyInfo::getCtime, bo.getCtime());
        lqw.eq(bo.getUuserId() != null, AuthCompanyInfo::getUuserId, bo.getUuserId());
        lqw.eq(bo.getUtime() != null, AuthCompanyInfo::getUtime, bo.getUtime());
        lqw.eq(bo.getDeleteStatus() != null, AuthCompanyInfo::getDeleteStatus, bo.getDeleteStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDes()), AuthCompanyInfo::getDes, bo.getDes());
        return lqw;
    }

    /**
     * 新增企业权限
     *
     * @param bo 企业权限
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AuthCompanyInfoBo bo) {
        AuthCompanyInfo add = MapstructUtils.convert(bo, AuthCompanyInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改企业权限
     *
     * @param bo 企业权限
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AuthCompanyInfoBo bo) {
        AuthCompanyInfo update = MapstructUtils.convert(bo, AuthCompanyInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AuthCompanyInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除企业权限信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public AuthCompanyInfoVo selectByAppId(Long appId) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<AuthCompanyInfo>().eq(AuthCompanyInfo::getAppId, appId));
    }
}
